package org.ocean.online.sys;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class OnlineConfig {

	private static Properties props=new Properties();;

	private static File file;
	private static Logger log = LoggerFactory.getLogger(OnlineConfig.class);
	public static void main(String[] args){
		
	}
	
	public static String getProperty(String key){
		if(props.containsKey(key)){
			return props.getProperty(key);
		}
		return "";
	}

	public static void put(Object key,Object value){
		props.put(key, value);
	}
	
	public static void save(){
		try {
			props.store(FileUtils.openOutputStream(file), "");
		} catch (IOException e) {
			throw new RuntimeException("保存配置信息失败",e);
		}
	}
	static Properties getProps(){
		return props;
	}
	public static void init() {
		String classesPath = OnlineConfig.class.getResource("/").getPath();
		System.out.println("classespath:"+classesPath);
		file= new File(classesPath+"mutiple.properties");
		if(file.exists()){
			try {
				props.load(new FileInputStream(file));
			}catch (Exception e) {
				throw new RuntimeException("加载配置信息失败",e);
				//log.error("加载配置信息失败",e);
			}
			return;
		}
		
		file = new File(classesPath+"simple.properties");
		if(file.exists()){
			try {
				props.load(new FileInputStream(file));
			}catch (Exception e) {
				throw new RuntimeException("加载配置信息失败",e);
				//log.error("加载配置信息失败",e);
			}
		}else{
			props.put("dbType", "h2");
			props.put("url", "");
			props.put("username", "");
			props.put("password", "");
			props.put("initd", "false");
			props.put("projectType", "simple");
			try {
				file.createNewFile();
				props.store(new FileOutputStream(file), "");
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
	}
}
